﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    layui.use(['jquery', 'form', 'table', 'tableEdit', 'common', 'commonTable', 'optimizeSelectOption'], function () {
        var form = layui.form,
            $ = layui.$,
            tableEdit = layui.tableEdit,
            table = layui.table,
            commonTable = layui.commonTable,
            common = layui.common;

        var keyValue = $.request("keyValue");
        var listData = [];
        var materialId = '';
        $(function () {
            if (!!keyValue) {
                $("#F_EqpId").bindSelect({
                    url: "/EquipmentManage/Equipment/GetListJson",
                    id: "F_Id",
                    text: "F_EqpName"
                });
                $("#F_EqpId").val(keyValue);
                //
                common.ajax({
                    url: '/EquipmentManage/Equipment/GetMaterialListJson',
                    dataType: 'json',
                    data: { keyValue: keyValue},
                    async: false,
                    success: function (data) {
                        if (!!data) {
                            listData = data;
                        }
                    }
                });
            }
            var params = [];
            var produceTypes = [{ name: '独立', value: '0' }, { name: '共享', value: '1' }]
            //生产物料
            common.ajax({
                url: '/EquipmentManage/Equipment/GetMaterialList',
                dataType: 'json',
                data: { keyValue: keyValue },
                async: false,
                success: function (data) {
                    if (!!data) {
                        for (var i = 0; i < data.length; i++) {
                            var temp = {};
                            temp.name = data[i].F_MaterialName;
                            temp.value = data[i].F_Id;
                            params.push(temp);
                        }
                    }
                }
            });
            var tablefilter = table.render({
                elem: '#currentTableId',
                data: listData,
                defaultToolbar: [],
                toolbar: '#toolbarDemo',//工具栏
                search: false,
                limit: 9999,//每页数据 默认
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count'] //自定义分页布局
                    , first: false //不显示首页
                    , last: false //不显示尾页
                },
                height: 'full-100',
                cols: [[
                    { type: "radio", width: 50 },
                    {
                        field: 'F_MaterialId', title: '物料名称', minWidth: 160, sort: true, event: 'selectMaterial', config: { type: 'select', data: params, verify: true }
                        , templet: function (d) {
                            if (d.F_MaterialId) {
                                if (d.F_MaterialId.value) {
                                    return d.F_MaterialId.name;
                                }
                                for (var i = 0; i < params.length; i++) {
                                    if (params[i].value == d.F_MaterialId) {
                                        return params[i].name;
                                    }
                                }
                            }
                            return ''
                        }
                    },
                    {
                        field: 'F_ProduceType', title: '生产模式', minWidth: 120, sort: true, event: 'selectProduceType', config: { type: 'select', data: produceTypes, verify: true }
                        , templet: function (d) {
                            if (d.F_ProduceType) {
                                if (d.F_ProduceType.value) {
                                    return d.F_ProduceType.name;
                                }
                                for (var i = 0; i < produceTypes.length; i++) {
                                    if (produceTypes[i].value == d.F_ProduceType) {
                                        return produceTypes[i].name;
                                    }
                                }
                            }
                            if (d.F_ProduceType==0) {
                                return '独立';
                            }
                            return '';
                        }
                    },
                    { field: 'F_Num', title: '数量', width:90, sort: true },
                    { field: 'F_Priority', title: '生产优先级', width: 130, edit: 'text', sort: true },
                ]]
            });
            var aopTable = tableEdit.aopObj(tablefilter.config.cols); //获取一个aop对象
            aopTable.on('tool(currentTableFilter)', function (obj) {
                var field = obj.field; //单元格字段
                var value = obj.value; //修改后的值
                var data = obj.data; //当前行旧数据
                var event = obj.event; //当前单元格事件属性值
                var update = {};
                update[field] = value;
                //把value更新到行中
                obj.update(update);
            });
            //行点击事件监听，控制按钮显示
            var oneList = ["NF-delete"];//选择1条显示
            commonTable.tableRowClick("radio", "currentTableFilter", "currentTableId", oneList);
            form.render();
        });
        wcLoading.close();
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var postData = data.field;
            var listdata = table.cache.currentTableId;
            if (listdata.length == 0) {
                common.modalMsg("请添加物料", "warning");
                return false;
            }
            postData.listData = [];
            for (var i = 0; i < listdata.length; i++) {
                var temp = {};
                temp.F_MaterialId = !!listdata[i].F_MaterialId.value ? listdata[i].F_MaterialId.value : listdata[i].F_MaterialId;
                temp.F_Num = listdata[i].F_Num;
                temp.F_ProduceType = !!listdata[i].F_ProduceType.value ? listdata[i].F_ProduceType.value : listdata[i].F_ProduceType;
                temp.F_Priority = listdata[i].F_Priority;
                temp.F_EqpId = listdata[i].F_EqpId;
                postData.listData.push(temp);
            }
            common.submitForm({
                url: '/EquipmentManage/Equipment/SubmitMaterialForm',
                param: postData,
                success: function () {
                    common.parentreload('data-search-btn');
                }
            })
            return false;
        });
        form.render();
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data;
            if (obj.event === 'add') {  // 监听添加操作
                var alldata = table.cache.currentTableId;
                alldata.push({ F_Id: uuid(), F_MaterialId: null, F_Num: '0', F_ProduceType: '0', F_Priority: '1' })
                table.reload('currentTableId', { data: alldata })
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                var alldata = table.cache.currentTableId;
                for (var i = 0; i < alldata.length; i++) {
                    if (alldata[i].F_Id == data[0].F_Id) {
                        alldata.splice(i, 1);
                        break;
                    }
                }
                table.reload('currentTableId', { data: alldata })
            }
            return false;
        });
    });
</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item">
                    <label class="layui-form-label required">设备</label>
                    <div class="layui-input-block">
                        <select id="F_EqpId" name="F_EqpId" disabled>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
                <div class="layui-form-item layui-hide">
                    <button class="layui-btn" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                    <button id="NF-add" name="NF-add" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe642;</i>添加</button>
                    <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger layui-hide" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>
                </div>
            </script>
        </div>
    </div>
</body>

